'use strict';

angular.module('snPaas').service('lineCharts',['ec',function(ec){


	var createChart = function(sel,name){

		var o = {};
		o.name = name;
		o.addData = function(data){
			this.chart.addData([
		        [
		            0,        // 系列索引
		            data[0], // 新增数据
		            false,    // 新增数据是否从队列头部插入
		            false,    // 是否增加队列长度，false则自定删除原有数据，队头插入删队尾，队尾插入删队头
		            data[1]  // 坐标轴标签
		        ]
		    ]);
		};
		o.batchAddData = function(data){
			var d = [];
			var t = [];
			for(var i=0,l = data.length;i<l;i++){
				d.push(data[i].value);
				t.push(data[i].date);
			}
			var opt = {
	          
	          tooltip : {
	              trigger: 'axis'
	          },
	          xAxis : [
	              {
	                  type : 'category',
	                  boundaryGap : false,
	                  data : t
	              }
	          ],
	          yAxis : [
	              {
	                  type : 'value'
	              }
	          ],
	          addDataAnimation:false,
	          animation:false,
	          series : [
	              {
	                  name:this.name,
	                  type:'line',
	                  smooth:true,
	                  itemStyle: {normal: {areaStyle: {type: 'default'}}},
	                  data:d
	              }
	          ]
	        };
			this.chart.clear();
			this.chart.setOption(opt);

		};
		o.clearData = function(){
			this.chart.clear();
			this.chart.setOption(option);
		};
		var option = {
	          // title : {
	          //     text: '某楼盘销售情况',
	          //     subtext: '纯属虚构'
	          // },
	          tooltip : {
	              trigger: 'axis',
	              formatter: "{a}{b}:{c}%"
	          },
	          // legend: {
	          //     data:['CPU']
	          // },
	          // toolbox: {
	          //     show : true,
	          //     feature : {
	          //         mark : {show: true},
	          //         dataView : {show: true, readOnly: false},
	          //         magicType : {show: true, type: ['line', 'bar', 'stack', 'tiled']},
	          //         restore : {show: true},
	          //         saveAsImage : {show: true}
	          //     }
	          // },
	          // calculable : true,
	          xAxis : [
	              {
	                  type : 'category',
	                  boundaryGap : false,
	                  data : ['','','','','','','','','','','','','','','','','','','','','','','','','','','','','','']
	              }
	          ],
	          yAxis : [
	              {
	                  type : 'value'
	              }
	          ],
	          addDataAnimation:false,
	          animation:false,
	          series : [
	              {
	              	  type:'line',
	                  name:o.name,
	                  smooth:true,
	                  itemStyle: {normal: {areaStyle: {type: 'default'}}},
	                  data:[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
	              }
	          ]
	      };
	      try{
	      	var myChart = echarts.init($(sel)[0]); 
			myChart.setOption(option);
			o.chart = myChart;
			return o;
		}
	      	
	      catch(e){
	      	console.log(e);
	      }
	    
	};


	return {createChart: createChart};
}])
.service('echartsOrigin',['ec',function(ec){
	var createChart = function(sel){
		var o = {};
		o.addData = function(data){
			this.chart.addData([
		        [
		            0,        // 系列索引
		            data[0], // 新增数据
		            false,    // 新增数据是否从队列头部插入
		            false,    // 是否增加队列长度，false则自定删除原有数据，队头插入删队尾，队尾插入删队头
		            data[1]  // 坐标轴标签
		        ]
		    ]);
		};
		o.clearData = function(){
			this.chart.clear();
			this.chart.setOption(option);
		};
		var option = {
	          // title : {
	          //     text: '某楼盘销售情况',
	          //     subtext: '纯属虚构'
	          // },
	          tooltip : {
	              trigger: 'axis'
	          },
	          // legend: {
	          //     data:['CPU']
	          // },
	          // toolbox: {
	          //     show : true,
	          //     feature : {
	          //         mark : {show: true},
	          //         dataView : {show: true, readOnly: false},
	          //         magicType : {show: true, type: ['line', 'bar', 'stack', 'tiled']},
	          //         restore : {show: true},
	          //         saveAsImage : {show: true}
	          //     }
	          // },
	          // calculable : true,
	          xAxis : [
	              {
	                  type : 'category',
	                  boundaryGap : false,
	                  data : ['','','','','','','','','','','','','','','','','','','','','','','','','','','','','','']
	              }
	          ],
	          yAxis : [
	              {
	                  type : 'value'
	              }
	          ],
	          addDataAnimation:false,
	          animation:false,
	          series : [
	              {
	                  name:'CPU',
	                  type:'line',
	                  smooth:true,
	                  itemStyle: {normal: {areaStyle: {type: 'default'}}},
	                  data:[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
	              }
	          ]
	      };
	      try{
	      	var myChart = echarts.init($(sel)[0]); 
			myChart.setOption(option);
			o.chart = myChart;
			return o;
		}
	      	
	      catch(e){
	      	console.log(e);
	      }
	    
	};


	return {createChart: createChart};
}])
;